#!/usr/bin/python
# Copyright (C) 2013, 2014 Christian Sengstock
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import sys
import unittest
from random import shuffle
sys.path.append("../src")
from fpgrowth import powersetminer, fim

def make_tdb(strings, shuffle_items=True):
    """Returns a transaction database from
    the given string arguments."""
    def f(x):
        x = list(set(x))
        if shuffle_items:
            shuffle(x)
        return tuple(x)

    return map(f, strings)

def order_res(res, joinitems=False):
    def f(x):
        items = list(x[0])
        items.sort()
        if joinitems:
            items = "".join(items)
        return items, x[1]
    res = map(f, res)
    res.sort(key=lambda x: x[0])  
    return res

class TestFPGrowth(unittest.TestCase):
    def setUp(self):
        pass

    def test_fpgrowth(self):        
        tdb = make_tdb(["abcd", "ad", "a", "ad", "acd", "d", "cd", "cb"])
        n = len(tdb)
        for mincount in xrange(9):
            val = powersetminer(tdb, mincount)
            val = order_res(val, True)
            res = fim(tdb, mincount)
            res = order_res(res, True)
            assert val == res

if __name__ == "__main__":
    unittest.main()
